import ISO6391 from 'iso-639-1'

export type Name = string

export type CName = string

export type NativeName = string

export type Iso = string

export type DetectLang = {
  cname: CName,
  iso: Iso,
}

export const isos = ['zh', 'ru', 'ja', 'th', 'ko', 'en', 'id', 'es', 'it', 'fr', 'de']

export const isoNameMap = isos.reduce<Record<Iso, Name>>((map, iso) => ({
  ...map,
  [iso]: ISO6391.getName(iso).toLowerCase(),
}), {})

export const nameIsoMap = isos.reduce<Record<Name, Iso>>((map, iso) => ({
  ...map,
  [isoNameMap[iso]]: iso,
}), {})

export const isoCnameMap: Record<Iso, CName> = {
  zh: '中文',
  ru: '俄语',
  ja: '日语',
  th: '泰语',
  en: '英文',
  id: '印尼语',
  es: '西班牙语',
  it: '意大利语',
  fr: '法语',
  de: '德语',
  ko: '韩语',
}

export const cnameIsoMap = isos.reduce<Record<CName, Iso>>((map, iso) => ({
  ...map,
  [isoCnameMap[iso]]: iso,
}), {})

export const isoNativeNameMap = isos.reduce<Record<Iso, NativeName>>((map, iso) => ({
  ...map,
  [iso]: ISO6391.getNativeName(iso),
}), {})

export const nativeNameIsoMap = isos.reduce<Record<NativeName, Iso>>((map, iso) => ({
  ...map,
  [isoNativeNameMap[iso]]: iso,
}), {})
